package com.zhm.demo.redis.redison;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class TestRedisson {

    public static RedissonClient getRedissonClient() {
        Config config = new Config();

        config.useSingleServer()
//                .setPassword(StringUtils.isEmpty(password) ? null : password)
                .setAddress("redis://172.24.73.24:6379");
        return Redisson.create(config);
    }

    public void testLock() throws InterruptedException {
        RedissonClient redissonClient = getRedissonClient();
        RLock rLock = redissonClient.getLock("testLock110");
        boolean b = rLock.tryLock(10, 20, TimeUnit.SECONDS);
        if (b) {
            String threadName = Thread.currentThread().getName();
            System.out.println("lock success " + threadName);
            Thread.sleep(10 * 1000);
        }
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                while (true) {
                    try {
                        new TestRedisson().testLock();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }
}
